package test5.lupeng.jf.com.teamdemo.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

import butterknife.BindView;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.homeFragment.ClassMessageFragment;
import test5.lupeng.jf.com.teamdemo.homeFragment.JFMessageFragment;
import test5.lupeng.jf.com.teamdemo.homeFragment.OtherMessageFragment;

/**
 * 首页
 * Created by C-yx on 2017/7/27.
 */

public class HomePageFragment extends Fragment {

    ArrayList<Fragment> fragments;
    ArrayList<String> titles;
    private TabLayout tab;
    private ViewPager homeVp;
    private HomeAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_homepagefragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        tab = view.findViewById(R.id.tabs);
        homeVp = view.findViewById(R.id.homeVp);

        titles = new ArrayList<>();

        titles.add("机蜂资讯");
        titles.add("开班信息");
        titles.add("行业杂烩");

       tab.setTabMode(TabLayout.MODE_FIXED);
       tab.addTab(tab.newTab().setText(titles.get(0)));
       tab.addTab(tab.newTab().setText(titles.get(1)));
       tab.addTab(tab.newTab().setText(titles.get(2)));

//        JFMessageFragment jf = new JFMessageFragment();
//        ClassMessageFragment cf = new ClassMessageFragment();
//        OtherMessageFragment of = new OtherMessageFragment();

        fragments = new ArrayList<>();
        fragments.add(new JFMessageFragment());
        fragments.add(new ClassMessageFragment());
        fragments.add(new OtherMessageFragment());


        adapter = new HomeAdapter(getFragmentManager(), fragments);
        homeVp.setAdapter(adapter);
        tab.setupWithViewPager(homeVp);

    }

    public class HomeAdapter extends FragmentPagerAdapter{

        ArrayList<Fragment> newFragments;

        public HomeAdapter(FragmentManager fm,ArrayList<Fragment> newFragments) {
            super(fm);
            this.newFragments = newFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return newFragments.get(position);
        }

        @Override
        public int getCount() {
            return newFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }

        @Override
        public long getItemId(int position) {
            int hashCode = newFragments.get(position).hashCode();
            return hashCode;
        }
    }


}
